<script setup>
import { watch } from 'vue'

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  title: {
    type: String,
    default: '提示'
  },
  message: {
    type: String,
    default: ''
  },
  confirmText: {
    type: String,
    default: '确定'
  },
  showCancel: {
    type: Boolean,
    default: false
  },
  cancelText: {
    type: String,
    default: '取消'
  }
})

const emit = defineEmits(['update:visible', 'confirm', 'cancel'])

const close = () => {
  emit('update:visible', false)
}

const handleConfirm = () => {
  emit('confirm')
  close()
}

const handleCancel = () => {
  emit('cancel')
  close()
}

// 监听 visible 变化，控制 body 滚动
watch(() => props.visible, (val) => {
  if (val) {
    document.body.style.overflow = 'hidden'
  } else {
    document.body.style.overflow = ''
  }
})
</script>

<template>
  <Teleport to="body">
    <Transition name="modal-fade">
      <div v-if="visible" class="modal-overlay" @click.self="close">
        <div class="modal-container" @click.stop>
          <div class="modal-header" v-if="title">
            <div class="modal-title">{{ title }}</div>
          </div>
          <div class="modal-body">
            <div class="modal-message">{{ message }}</div>
          </div>
          <div class="modal-footer">
            <button v-if="showCancel" class="modal-btn cancel-btn" @click="handleCancel">
              {{ cancelText }}
            </button>
            <button class="modal-btn confirm-btn" @click="handleConfirm">
              {{ confirmText }}
            </button>
          </div>
        </div>
      </div>
    </Transition>
  </Teleport>
</template>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 16px;
}

.modal-container {
  background: #fff;
  border-radius: 16px;
  width: 100%;
  max-width: 320px;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  animation: modal-scale 0.3s ease-out;
}

@keyframes modal-scale {
  from {
    transform: scale(0.9);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

.modal-header {
  padding: 20px 20px 12px;
  text-align: center;
  border-bottom: 1px solid #f5f5f5;
}

.modal-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

.modal-body {
  padding: 20px;
  text-align: center;
}

.modal-message {
  font-size: 15px;
  color: #666;
  line-height: 1.6;
  white-space: pre-line;
  word-break: break-word;
}

.modal-footer {
  display: flex;
  border-top: 1px solid #f5f5f5;
}

.modal-btn {
  flex: 1;
  padding: 14px;
  border: none;
  background: transparent;
  font-size: 16px;
  cursor: pointer;
  transition: background 0.2s;
}

.modal-btn:active {
  background: #f5f5f5;
}

.cancel-btn {
  color: #666;
  border-right: 1px solid #f5f5f5;
}

.confirm-btn {
  color: #1890ff;
  font-weight: 600;
}

.modal-footer:has(.cancel-btn) .confirm-btn {
  border-right: none;
}

/* 过渡动画 */
.modal-fade-enter-active,
.modal-fade-leave-active {
  transition: opacity 0.3s;
}

.modal-fade-enter-from,
.modal-fade-leave-to {
  opacity: 0;
}

.modal-fade-enter-active .modal-container,
.modal-fade-leave-active .modal-container {
  transition: transform 0.3s, opacity 0.3s;
}

.modal-fade-enter-from .modal-container,
.modal-fade-leave-to .modal-container {
  transform: scale(0.9);
  opacity: 0;
}
</style>


